package com.hxd.otc.mvp.login;

import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.my.bean.ImageCodeBean;
import com.hxd.otc.module.my.contract.ForgetPasswordContract;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.UIUtils;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/6 10:19
 * 描  述 :  ${忘记密码Presenter}
 */
public class ForgetPasswordPresenter extends BasePresenter<ForgetPasswordContract.View> implements ForgetPasswordContract.Presenter {
    private UserModel mUserModel;

    public ForgetPasswordPresenter(ForgetPasswordContract.View view) {
        super(view);
        mUserModel = UserModel.getInstance();

    }

    @Override
    public void getPhoneVerCode(String nationalCode, String phone, String sessionId, String countryId) {
        mUserModel.sendPhoneCodeWithVerif(new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {
                mView.getVerCodeSuccess(msg);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose(), phone, "reset", "Android", "register", sessionId, countryId);
    }

    @Override
    public void forgetPasswordOfPhone(String mobile, String nationalCode, String password, String verCode, String countryId) {
        mUserModel.forgetPasswordOfPhone(mobile, password, verCode, countryId, new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String str) {
                mView.showToast(msg);
                mView.modificationSucceed();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean<String>>bindAutoDispose());
    }

    @Override
    public void getEmailVerCode(String email, String sessionId) {
        mUserModel.getEmailCode(new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {
                mView.getEmailVerCodeSuccess();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose(), email, "reset", "Android", "register", sessionId);
    }

    @Override
    public void findUser(final String account, final int registerMode) {
        mUserModel.findUser(account, new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {

            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {

            }

            @Override
            public void onNext(BaseBean<ImageCodeBean> response) {
//                super.onNext(response);
                if (response.code == BaseObserver.SUCCESS) {//该用户已注册
                    mView.checkNewPhoneFail(UIUtils.getContext().getString(R.string.please_input_right_phone));
                } else if (response.code == 10004042) {
                    mView.checkNewPhoneSuccess(account, registerMode);
                }else {
                    mView.checkNewPhoneFail(response.getMsg());
                }
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose());
    }


}
